package com.caigp.cookbook2.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.List;

public class JsonUtil {

    private static Gson gson = new Gson();

    private JsonUtil() {
    }

    public static Gson getInstance() {
        return gson;
    }

    public static <T> List<T> getList(String jsonArr, Class<T> cls) {
        List<T> list = new ArrayList<>();
        JsonArray jsonArray = JsonParser.parseString(jsonArr).getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            list.add(gson.fromJson(jsonElement, cls));
        }
        return list;
    }
}
